# Maintainer: Martell Malone <martellmalone@gmail.com>

_realname=flatbuffers
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=23.1.4
pkgrel=1
pkgdesc='Memory Efficient Serialization Library (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://google.github.io/flatbuffers/'
license=('spdx:Apache-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-numpy")
depends=("${MINGW_PACKAGE_PREFIX}-libsystre")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/google/${_realname}/archive/v${pkgver}.tar.gz"
        0001-cmake-fix-install.patch
        0002-cmake-prevent-libcxxabi.patch)
sha256sums=('801871ff3747838c0dd9730fc44ca9cc453ff42f9c8a0a2f1b33776d2ca5e4b9'
            '3a7900943c5a95a0350052ed29a982c65bc821ca50f0f56a7b9482e6361a10f0'
            'b33d9a0bf2d87f0948a83deac699b94ac4592f8aa422eb45961b36b768edd72e')
noextract=(${_realname}-${pkgver}.tar.gz)

prepare() {
  plain "Extracting ${_realname}-${pkgver}.tar.gz"
  tar -xzf ${_realname}-${pkgver}.tar.gz || true

  cd "${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/0001-cmake-fix-install.patch"
  patch -Np1 -i "${srcdir}/0002-cmake-prevent-libcxxabi.patch"
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_BUILD_TYPE=Release \
    -DFLATBUFFERS_BUILD_SHAREDLIB=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./

  # Python bindings
  cd "${srcdir}/${_realname}-${pkgver}/python"
  VERSION="${pkgver}" "${MINGW_PREFIX}"/bin/python setup.py build
}

package_flatbuffers() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install ./

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt"
}

package_python-flatbuffers() {
  pkgdesc='An efficient cross platform serialization library for Python (mingw-w64)'
  depends=("${MINGW_PACKAGE_PREFIX}-python")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python-numpy: support for Numpy arrays")

  cd "${srcdir}/${_realname}-${pkgver}/python"
    VERSION="${pkgver}" \
    MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    "${MINGW_PREFIX}"/bin/python setup.py install --prefix="${MINGW_PREFIX}" \
      --root="${pkgdir}" --optimize=1 --skip-build
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
